package com.xbongbong.paas.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleAddDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleDelDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleGetDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleListDTO;
import com.xbongbong.paas.statistics.pojo.dto.AggRuleUpdateDTO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleGetVO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleListVO;
import com.xbongbong.paas.statistics.pojo.vo.AggRuleSaveVO;
import com.xbongbong.paas.statistics.service.AggRuleService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 创建时间： 2018/9/18 11:26
 * 修改时间： 2018/9/18 11:26 梁鲁江
 *
 * @author 梁鲁江
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/statistics/rule")
public class AggRuleController {
    @Resource
    private AggRuleService aggRuleService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String insert(@RequestBody @Valid AggRuleAddDTO aggRuleAddDTO, BindingResult br) {
        XbbResponse<AggRuleSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AggRuleSaveVO aggRuleSaveVO = aggRuleService.addPaasAggRule(aggRuleAddDTO);
                response = new XbbResponse<>(aggRuleSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid AggRuleUpdateDTO aggRuleUpdateDTO, BindingResult br) {
        XbbResponse<AggRuleSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AggRuleSaveVO aggRuleSaveVO = aggRuleService.updatePaasAggRule(aggRuleUpdateDTO);
                response = new XbbResponse<>(aggRuleSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid AggRuleDelDTO aggRuleDelDTO, BindingResult br) {
        XbbResponse<AggRuleSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AggRuleSaveVO aggRuleSaveVO = aggRuleService.delPaasAggRule(aggRuleDelDTO);
                response = new XbbResponse<>(aggRuleSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid AggRuleGetDTO aggRuleGetDTO, BindingResult br) {
        XbbResponse<AggRuleGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AggRuleGetVO aggRuleGetVO = aggRuleService.getPaasAggRule(aggRuleGetDTO);
                response = new XbbResponse<>(aggRuleGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid AggRuleListDTO aggRuleListDTO, BindingResult br) {
        XbbResponse<AggRuleListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AggRuleListVO aggRuleListVO = aggRuleService.listPaasAggRule(aggRuleListDTO);
                response = new XbbResponse<>(aggRuleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
